package com.hanggold.ppt;

import com.spire.presentation.Presentation;
import com.spire.presentation.collections.SlideCollection;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author <a href="mailto:sunmch@163.com">smc</a>
 * @date 2019-09-18 10:00
 * @since 1.0.0
 **/
public class PPT2Image {

    private static String PPT_FILE="H:" + File.separator + "123.pptx";

    private static String IMAGE_DIR="H:" + File.separator + "ppt2img" + File.separator;

    private static String PPT_FILE_PROP_NAME="ppt.file";

    private static String PPT_TO_IMG_DIR="ppt.image.dir";

    public static void main(String[] args) throws Exception {

        PPT_FILE = System.getProperty(PPT_FILE_PROP_NAME,PPT_FILE);

        IMAGE_DIR = System.getProperty(PPT_TO_IMG_DIR,IMAGE_DIR);

        System.out.printf("ppt文件名称: %s \n",PPT_FILE);

        System.out.printf("ppt生成的图片目录: %s \n",IMAGE_DIR);

        long start = System.currentTimeMillis();

        Presentation ppt = new Presentation();

        ppt.loadFromFile(PPT_FILE);

        SlideCollection slideCollection =  ppt.getSlides();
        //Save PPT document to images
        for (int i = 0; i < slideCollection.getCount(); i++) {
            BufferedImage image =slideCollection.get(i).saveAsImage();
            ImageIO.write(image, "JPG",new File(IMAGE_DIR+ (i) + ".jpg"));
        }
        ppt.dispose();
//        FileOutputStream fileOutputStream = new FileOutputStream("H:" + File.separator + "yyy.pdf");
//
//        ppt.saveToFile(fileOutputStream, FileFormat.PDF);
//        fileOutputStream.flush();
//        fileOutputStream.close();
//        ppt.dispose();
        long end = System.currentTimeMillis();
        System.out.printf("转换pdf花费时间 :  %d \n", (end - start));



    }
}
